package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.WendaQuestion;
import cn.wolfcode.luowowo.article.domain.WendaQuestionContent;
import cn.wolfcode.luowowo.article.mapper.WendaQuestionContentMapper;
import cn.wolfcode.luowowo.article.mapper.WendaQuestionMapper;
import cn.wolfcode.luowowo.article.query.WendaQuestionQuery;
import cn.wolfcode.luowowo.article.service.IWendaQuestionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class WendaQuestionServiceImpl implements IWendaQuestionService {

    @Autowired
    private WendaQuestionMapper wendaQuestionMapper;
    @Autowired
    private WendaQuestionContentMapper wendaQuestionContentMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return wendaQuestionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(WendaQuestion wendaQuestion) {
        return wendaQuestionMapper.insert(wendaQuestion);
    }

    @Override
    public WendaQuestion selectByPrimaryKey(Long id) {
        return wendaQuestionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<WendaQuestion> selectAll() {
        return wendaQuestionMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(WendaQuestion wendaQuestion) {
        return wendaQuestionMapper.updateByPrimaryKey(wendaQuestion);
    }

    @Override
    public Long saveOrUpdate(WendaQuestion wendaQuestion, Long tagId) {
        if(wendaQuestion.getId() == null){
            //保存操作
            //创建时间
            wendaQuestion.setCreateTime(new Date());
            wendaQuestion.setReplyTime(new Date());
            wendaQuestionMapper.insert(wendaQuestion);
            WendaQuestionContent content = wendaQuestion.getWendaQuestionContent();
            content.setId(wendaQuestion.getId());
            wendaQuestionContentMapper.insert(content);

        }else{
            wendaQuestionMapper.updateByPrimaryKey(wendaQuestion);

            WendaQuestionContent content = wendaQuestion.getWendaQuestionContent();
            content.setId(wendaQuestion.getId());
            wendaQuestionContentMapper.updateByPrimaryKey(content);
        }
        // 维护问题和标签的关系  tagId
        wendaQuestionMapper.insertQuestionAndTagRelation(wendaQuestion.getId(),tagId);
        return wendaQuestion.getId();
    }

    @Override
    public WendaQuestionContent getContent(Long id) {
        return wendaQuestionContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo query(WendaQuestionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //,qo.getOrderBy()+" desc"
        return new PageInfo(wendaQuestionMapper.selectForList(qo));
    }

    @Override
    public PageInfo queryConcat(WendaQuestionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(wendaQuestionMapper.selectForQueryList(qo));
    }
}
